/*******************************************************************************
*   			Impacts of a Large-Scale Parenting Program: 				   *
*					Experimental Evidence from Chile						   *
********************************************************************************


	REQUIRES:	"${dt_final}panel_RR_indexes.dta"
	CREATES:	${out_tables}/T13A_iv_child_individual.xls
				
	WRITEN BY:  Italo Lopez  [italolop@usc.edu]

********************************************************************************
	Prepare data
*******************************************************************************/

	* Load data
	use "${dt_final}/panel_RR_indexes.dta", clear


	* Establish macros for regressions

/*******************************************************************************
*   Set macros for regressions
*******************************************************************************/
	
	* Outcomes
	*-----------------------------------------------------------
	local childoutcomes  "zDCCSPunt zptevir_irtscore2 zt_score zcbcl_t_e zcbcl_t_i zibatt_sspi zibatt_ssai zibatt_sssr zas_raw_score" 
	local behaviors "home_score se_score disc_score" 
	local beliefs "zpstyle1 zpstyle2 zpstyle3 zPSCS zPSSS_family zPSSS_friends zPSSS_others zPSI_Distress zCESD zpacotis"	
	local child "zptevir_irtscore2 zcog_index zsoc_index" 

	* Controls
	*-----------------------------------------------------------

	macro  def Xvar1 "i.age_year i.gender"  	
	macro  def Xvar2 "i.age_year i.gender inc_qtaut_old i.pc_edu_mdsfin hh_mem hh_tipo"  	
	macro  def Xvar2old "i.gender inc_qtaut_old i.pc_edu_mdsfin_old hh_mem hh_tipo"  

	
	* Parental characteristics: WAIS and BFI and imputed wais 
	*-----------------------------------------------------------

	macro def parent_cha4 "BFI_ext BFI_ope BFI_con BFI_neu BFI_agr wais2 flag_wais"
	
	


/*******************************************************************************
	 Table A13: IV estimates of child individual measures
*******************************************************************************/


	local measures "zptevir_irtscore2 zDCCSPunt zas_raw_score zibatt_sspi zibatt_ssai zibatt_sssr zcbcl_t_t zcbcl_t_e zcbcl_t_i zcbcl_p_1 zcbcl_p_2 zcbcl_p_3 zcbcl_p_4 zcbcl_p_5 zcbcl_p_6 zcbcl_p_7"
	local list1 "measures" 
	#delimit;
	foreach f of local list1 {;

			foreach var of local `f' {;
			
				di "`var'";
					
				* PANEL 1: ITT with Age and Gender ;
					xi: xtivreg `var' $Xvar1 (treat2 treat3=group2 group3),  fe i(CENTRO_SALUD);
					estimates store `var'_iv1;
					test treat2=treat3;
					estadd scalar pb=r(p);				

				* PANEL 2: ITT with Age and Gender and parental characteristics ;
					xi: xtivreg `var' $Xvar2 $parent_cha4  (treat2 treat3=group2 group3),  fe i(CENTRO_SALUD);
					estimates store `var'_iv2;
					test treat2=treat3;
					estadd scalar pb=r(p);		
					
				* PANEL 3: ITT plus baseline outcome ;
					xi: xtivreg `var'  $Xvar2 $parent_cha4 zchild_index_old  (treat2 treat3=group2 group3),  fe i(CENTRO_SALUD);
					estimates store `var'_iv3;
					test treat2=treat3;
					estadd scalar pb=r(p);				
					
								};
			
	};



	#delimit cr
	***  Tables ITT Outcomes
		esttab zptevir_irtscore2_iv1 zDCCSPunt_iv1 zas_raw_score_iv1      	  /*
		*/ zcbcl_t_t_iv1 zcbcl_t_e_iv1 zcbcl_t_i_iv1 	  /*
		*/ zcbcl_p_1_iv1 zcbcl_p_2_iv1 zcbcl_p_3_iv1 zcbcl_p_4_iv1 zcbcl_p_5_iv1 /*
		*/ zcbcl_p_6_iv1  zcbcl_p_7_iv1       	  /*   
		*/ zibatt_sssr_iv1 zibatt_ssai_iv1 zibatt_sspi_iv1  /*
		*/ using "${out_tables}/appendix/T13A_iv_child_individual.csv", replace f		  /* 
		*/ keep(treat2 treat3) 											  /*
		*/ cells("b(fmt(3)star)" "se(fmt(3)par)") 							  /*
		*/ star(* 0.10 ** 0.05 *** 0.01) 			legend					  /*
		*/ stats(r2_o pb N mean_y sd_y, fmt(2 3 0 ) labels("R-squared" "P value for B=I" "Observations")) 	 /*
		*/ mtitles("TEVI-R (language)""DCCS (cognitive flexibility)" 		  /*
		*/ "Leiter-R (sustained attention)" "CBCL total (behavior)" 		  /*
		*/ "CBCL externalization" "CBCL internalization" "CBCL Emotional"	  /*
		*/ "CBCL Anxious""CBCL Somatic" "CBCL Withdrawn" "CBCL Sleep problems" /*
		*/ "CBCL Attention problems" "CBCL Aggressive" "Battelle Social Role" /*
		*/ "Battelle Interaction w/Adults" "Battelle Interaction w/Peers" )   /*
		*/ title("PANEL 1: iv with Age and Gender") 	varlabels(treat2 "NEP-B" treat3 "NEP-I")
		

		esttab zptevir_irtscore2_iv2 zDCCSPunt_iv2 zas_raw_score_iv2		  /*
		*/  zcbcl_t_t_iv2  	  /*
		*/ zcbcl_t_e_iv2 zcbcl_t_i_iv2 zcbcl_p_1_iv2 zcbcl_p_2_iv2 		  	  /*
		*/ zcbcl_p_3_iv2 zcbcl_p_4_iv2 zcbcl_p_5_iv2 zcbcl_p_6_iv2		  	  /*
		*/ zcbcl_p_7_iv2 zibatt_sssr_iv2 zibatt_ssai_iv2 zibatt_sspi_iv2 /*
		*/ using "${out_tables}/appendix/T13A_iv_child_individual.csv", append f		  /* 
		*/ keep(treat2 treat3) 											  /*
		*/ cells("b(fmt(3)star)" "se(fmt(3)par)") 							  /*
		*/ star(* 0.10 ** 0.05 *** 0.01) 			legend					  /*
		*/	stats(r2_o pb N , fmt(2 3 0) labels("R-squared" "P value for B=I"  "Observations" ))	/*
		*/ mtitles("TEVI-R (language)""DCCS (cognitive flexibility)" 		  /*
		*/ "Leiter-R (sustained attention)" "CBCL total (behavior)" 		  /*
		*/ "CBCL externalization" "CBCL internalization" "CBCL Emotional"	  /*
		*/ "CBCL Anxious""CBCL Somatic" "CBCL Withdrawn" "CBCL Sleep problems" /*
		*/ "CBCL Attention problems" "CBCL Aggressive" "Battelle Social Role" /*
		*/ "Battelle Interaction w/Adults" "Battelle Interaction w/Peers" )   /*
		*/ title(" PANEL 2: iv with Age and Gender and parental characteristics") 	varlabels(treat2 "NEP-B" treat3 "NEP-I")

		esttab zptevir_irtscore2_iv3 zDCCSPunt_iv3 zas_raw_score_iv3 	  	  /*
		*/ zcbcl_t_t_iv3  	  /*
		*/ zcbcl_t_e_iv3 zcbcl_t_i_iv3 zcbcl_p_1_iv3 zcbcl_p_2_iv3 		  	  /*
		*/ zcbcl_p_3_iv3 zcbcl_p_4_iv3 zcbcl_p_5_iv3 zcbcl_p_6_iv3 		  	  /*
		*/ zcbcl_p_7_iv3 zibatt_sssr_iv3 zibatt_ssai_iv3 zibatt_sspi_iv3 	 /*
		*/ using "${out_tables}/appendix/T13A_iv_child_individual.csv", append f /* 
		*/ keep(treat2 treat3) 											      /*
		*/ cells("b(fmt(3)star)" "se(fmt(3)par)") 							  /*
		*/ star(* 0.10 ** 0.05 *** 0.01) 			legend					  /*
		*/ stats(r2_o pb N, fmt(2 3 0)  /*
		*/ labels("R-squared" "P value for B=I" "Observations")) 	  /*
		*/ mtitles("TEVI-R (language)""DCCS (cognitive flexibility)" 		  /*
		*/ "Leiter-R (sustained attention)" "CBCL total (behavior)" 		  /*
		*/ "CBCL externalization" "CBCL internalization" "CBCL Emotional"	  /*
		*/ "CBCL Anxious""CBCL Somatic" "CBCL Withdrawn" "CBCL Sleep problems" /*
		*/ "CBCL Attention problems" "CBCL Aggressive" "Battelle Social Role" /*
		*/ "Battelle Interaction w/Adults" "Battelle Interaction w/Peers" )   /*
		*/ title("PANEL 3: iv plus baseline outcome") 	varlabels(treat2 "NEP-B" treat3 "NEP-I")
		
	
